package com.ts.api.module.match.plan.internal.distribute.model;

import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.IteratorUtils;
import com.ts.api.common.constant.productflow.ProductApplyFlowStatusEnum;
import com.ts.api.module.api.convert.platform.entity.RespPlatformMultiPhoneMaskDataExchange;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMaskDataExchange;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMd5DataExchange;
import com.ts.api.module.product.entity.po.ProductPO;
import com.ts.api.module.product.entity.po.ProductPlanPO;
import lombok.Data;

import java.util.List;

/**
 *  计划分发字段对象
 *
 *  @author Pan
 */
@Data
public class PlanDistributeField {

    /** 手机号md5模式返回字段 */
    private PlanDistributePhoneMd5Field phoneMd5Field;
    /** 手机号掩码模式返回字段-多个模式 */
    private List<PlanDistributePhoneMaskField> listPhoneMaskField;
    /** 是否解析成功 */
    private Boolean parseStatus = false;
    /** 业务错误信息 */
    private String bizErrorMsg;
    /** 业务错误返回状态 */
    private ProductApplyFlowStatusEnum bizErrorType;

    /**
     *  构建自定义参数
     *
     *  @return PlanDistributeFieldResult
     */
    public static PlanDistributeField of() {
        return new PlanDistributeField();
    }

    /**
     *  构建失败
     *
     *  @return PlanDistributeFieldResult
     */
    public static PlanDistributeField ofFail(String bizErrorMsg) {
        PlanDistributeField planDistributeField = new PlanDistributeField();
        planDistributeField.setBizErrorMsg(bizErrorMsg);
        return planDistributeField;
    }

    /**
     *  构建失败
     *
     *  @return PlanDistributeFieldResult
     */
    public static PlanDistributeField ofFail(String bizErrorMsg, ProductApplyFlowStatusEnum productApplyFlowStatusEnum) {
        PlanDistributeField planDistributeField = new PlanDistributeField();
        planDistributeField.setBizErrorType(productApplyFlowStatusEnum);
        planDistributeField.setBizErrorMsg(bizErrorMsg);
        return planDistributeField;
    }

    /**
     *  构建机构字段返回对象
     *
     *  @param  productPO       主体
     *  @param  productPlanPO   主体计划
     *  @param  reqId           本次请求id
     *  @return PlanDistributeFieldResult
     */
    public static PlanDistributeField ofOrgPhoneMd5(ProductPO productPO, ProductPlanPO productPlanPO, String reqId) {
        PlanDistributeField planDistributeField = new PlanDistributeField();

        //  返回字段
        PlanDistributePhoneMd5Field phoneMd5Field = new PlanDistributePhoneMd5Field();
        phoneMd5Field.setReqId(reqId);
        phoneMd5Field.setApplyId(reqId);
        phoneMd5Field.setProductName(productPO.getName());
        phoneMd5Field.setDisplayName(productPlanPO.getDisplayName());
        phoneMd5Field.setPrice(productPlanPO.getPrice());
        phoneMd5Field.setLink(productPlanPO.getLink());
        phoneMd5Field.setLinkType(productPlanPO.getLinkType());
        phoneMd5Field.setLogoPath(productPlanPO.getLogoPath());

        planDistributeField.setPhoneMd5Field(phoneMd5Field);
        planDistributeField.setParseStatus(true);
        return planDistributeField;
    }

    /**
     *  构建机构字段返回对象
     *
     *  @param  productPO       主体
     *  @param  productPlanPO   主体计划
     *  @param  listPhoneMd5    返回的md5集合
     *  @param  reqId           本次请求id
     *  @param  applyId         进件请求id
     *  @return PlanDistributeFieldResult
     */
    public static PlanDistributeField ofOrgPhoneMask(ProductPO productPO, ProductPlanPO productPlanPO, List<String> listPhoneMd5, String reqId, String applyId) {
        PlanDistributeField planDistributeField = PlanDistributeField.of();

        //  返回字段
        List<PlanDistributePhoneMaskField> singleList = CollUtils.newArrayList();

        PlanDistributePhoneMaskField orgPhoneMaskField = new PlanDistributePhoneMaskField();
        orgPhoneMaskField.setReqId(reqId);
        orgPhoneMaskField.setApplyId(applyId);
        orgPhoneMaskField.setProductName(productPO.getName());
        orgPhoneMaskField.setDisplayName(productPlanPO.getDisplayName());
        orgPhoneMaskField.setPrice(productPlanPO.getPrice());
        orgPhoneMaskField.setLink(productPlanPO.getLink());
        orgPhoneMaskField.setLinkType(productPlanPO.getLinkType());
        orgPhoneMaskField.setLogoPath(productPlanPO.getLogoPath());
        orgPhoneMaskField.setListPhoneMd5(listPhoneMd5);
        singleList.add(orgPhoneMaskField);

        planDistributeField.setListPhoneMaskField(singleList);
        planDistributeField.setParseStatus(true);
        return planDistributeField;
    }

    /**
     *  构建平台字段返回对象
     *
     *  @param  apiPlatFormDataExchange       平台返回对象
     *  @param  reqId                         本次请求id
     *  @return PlanDistributeFieldResult
     */
    public static PlanDistributeField ofPlatformPhoneMd5(RespPlatformPhoneMd5DataExchange apiPlatFormDataExchange, String reqId) {
        PlanDistributeField planDistributeField = new PlanDistributeField();

        //  返回字段
        PlanDistributePhoneMd5Field phoneMd5Field = new PlanDistributePhoneMd5Field();
        phoneMd5Field.setReqId(reqId);
        phoneMd5Field.setLogoPath(apiPlatFormDataExchange.getLogoPath());
        //  主体名称 -> 公司名称名称
        phoneMd5Field.setProductName(apiPlatFormDataExchange.getProductName());
        //  投放名称 -> 产品名称
        phoneMd5Field.setDisplayName(apiPlatFormDataExchange.getDisplayName());
        phoneMd5Field.setApplyId(apiPlatFormDataExchange.getApplyId());
        phoneMd5Field.setCallUrl(apiPlatFormDataExchange.getCallUrl());
        //  原始价格
        phoneMd5Field.setPrice(apiPlatFormDataExchange.getPrice());
        phoneMd5Field.setProtocolList(apiPlatFormDataExchange.getProtocolList());

        planDistributeField.setPhoneMd5Field(phoneMd5Field);
        planDistributeField.setParseStatus(true);
        return planDistributeField;
    }

    /**
     *  构建平台字段返回对象
     *
     *  @param  reqId                         本次请求id
     *  @return PlanDistributeFieldResult
     */
    public static PlanDistributeField ofPlatformPhoneMask(RespPlatformMultiPhoneMaskDataExchange resp, String reqId) {
        List<RespPlatformPhoneMaskDataExchange> listRespPlatformPhoneMaskDataExchange = resp.getListPhoneMask();

        PlanDistributeField planDistributeField = new PlanDistributeField();
        List<PlanDistributePhoneMaskField> listPhoneMaskFieldResult = CollUtils.newArrayList();

        //  返回响应判断
        IteratorUtils.array(listRespPlatformPhoneMaskDataExchange, phoneMaskResult -> {
            //  构建返回实体
            PlanDistributePhoneMaskField phoneMaskField = PlanDistributePhoneMaskField.of();

            phoneMaskField.setReqId(reqId);
            phoneMaskField.setApplyId(phoneMaskResult.getApplyId());
            phoneMaskField.setLogoPath(phoneMaskResult.getLogoPath());
            //  主体名称 -> 公司名称名称
            phoneMaskField.setProductName(phoneMaskResult.getProductName());
            //  投放名称 -> 产品名称
            phoneMaskField.setDisplayName(phoneMaskResult.getDisplayName());
            phoneMaskField.setListPhoneMd5(phoneMaskResult.getListPhoneMd5());
            phoneMaskField.setCallUrl(phoneMaskResult.getCallUrl());
            //  原始价格
            phoneMaskField.setPrice(phoneMaskResult.getPrice());
            phoneMaskField.setProtocolList(phoneMaskResult.getProtocolList());
            listPhoneMaskFieldResult.add(phoneMaskField);
        });

        planDistributeField.setListPhoneMaskField(listPhoneMaskFieldResult);
        planDistributeField.setParseStatus(true);
        return planDistributeField;
    }

}
